/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.model;

import foxmob.mlts.component.StringManager;
import java.util.*;

/**
 *
 * @author Emmanouil Samatas
 */
public class QuestionPaper {
    private ArrayList<Section> sections;
    private String testName;
    private float score;
    private String ID;
    private String description;
    
    
    public QuestionPaper() {
       sections = new ArrayList<Section>();
       UUID testId = UUID.randomUUID();
       ID = testId.toString();
    }
    
    @Override
    public String toString() {
        return this.testName;
    }
    
    public String getId(){
        return this.ID;
    }
    /*
     * Set the section ArrayList with the given arrayList
     */
    public void setSections(ArrayList<Section> sections) {
        this.sections = sections;
    }
    /*
     * Get the sections ArrayList contained to an EnglishTest object
     */
    public ArrayList<Section> getSections() {
        return sections;
    }
    /*
     * Adds a specific new section to the sections array
     * Updates the id of the new added section
     */
    public void addSection(Section section) {
        // add to section array
        // set section ID by adding 1 of the current section length
       
        this.sections.add(section);
    }
    
    /*
     * Adds an array of section to the english test object
     */
    public void addSection(ArrayList<Section> sections) {
        // loop through a list of section
        for (Section sec : sections) {
            addSection(sec);
        }
    }
    
    public void removeSection(Section section) {
        this.sections.remove(section);
    }
    
    public void removeSection(ArrayList<Section> section) {
        // loop through a list of sections
        for (Section sec : section) {
            removeSection(sec);
        }
    }
    
    public boolean isExistSection(Section section) {
        for (Section sec : this.sections) {
            if (sec.getTitle().equals(section.getTitle())) {
                return true;
            }
        }
        return false;
    }
    
    public void modifySection(Section section) {
        int i = 0;
        for (Section q : sections) {
            if (q.getID().equals(section.getID())) {
                 break;
            }
            ++i;
        } 
        this.sections.set(i, section);
    }
    
    public int getSectionsLength() {
        return sections.size();
    }
    
    public Section getSectionWithID(String ID) {
        for (Section sec : sections) {
            if (sec.getID().equals(ID)) {
                return sec; 
            }
        }
        return null;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getDescription() {
        return this.description;
    }
    
    public void setTestName(String testName) {
        this.testName = testName;
    }
    
    public String getTestName() {
        return testName;
    }
    
    public void setScore(float score) {
        this.score = score;
    }
    
    public float getScore() {
        return score;
    }
    public ArrayList<Question> getAllQuestions(){
        ArrayList<Question> questions = new ArrayList();
        for(Section sec:sections) {
            for(Subsection sub :sec.getSubsections()) {
                questions.addAll(sub.getQuestions());
            }
        }
              return questions;  
    }
    public ArrayList<Question> getQuestionsInSection(String sectionTitle){
        ArrayList<Question> questions = new ArrayList();
        for(Section sec:sections){
            if (sec.getTitle().equalsIgnoreCase(sectionTitle))
            {
              
            for(Subsection sub :sec.getSubsections()) {
                questions.addAll(sub.getQuestions());
                 }  
            }
        }
        return questions;
    }
    
    
      @Override
    public boolean equals(Object obj)
    {       
        boolean isEqual=false;
        if (this.getClass() == obj.getClass())
        {            
           QuestionPaper myTest = (QuestionPaper) obj;
           isEqual =(myTest.getAllQuestions().equals(this.getAllQuestions()) && (myTest.getId().equals(this.ID)))?true:false;  
        }
        return isEqual;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (this.sections != null ? this.sections.hashCode() : 0);
        hash = 67 * hash + (this.testName != null ? this.testName.hashCode() : 0);
        hash = 67 * hash + Float.floatToIntBits(this.score);
        return hash;
    }
}
